package com.javasm.demobike;

import java.util.Arrays;
import java.util.Scanner;

public class ShareBikeManager {

    //初始化3家公司的数组
    private ShareBikeCompany[]  shareBikeCompanies;
    static Scanner scanner = new Scanner(System.in);

    public void menu(){


        init();

        String answer;

        do{
            System.out.println("***************欢迎使用共享单车系统*******************");
            System.out.println("1投放单车");
            System.out.println("2查看单车");
            System.out.println("3删除单车");
            System.out.println("4借出单车");
            System.out.println("5归还单车");
            System.out.println("6单车排行榜");
            System.out.println("7退出");
            System.out.println("请输入");
            int sum = scanner.nextInt();
            switch(sum){
                case 1:
                    addBiike();
                    break;
                case 2:
                    queryBike();
                    break;
                case 3:
                    removeBike();
                    break;
                case 4:
                    borrowBike();
                    break;
                case 5:
                    backBike();
                    break;
                case 6:
                    rank();
                    break;
                case 7:
                    System.out.println("感谢使用共享单车系统，再见1！");
                    System.exit(0); // 添加退出程序的代码
                    break;


            }
            System.out.println("是否继续投放？Y/N?");
            answer = scanner.next();
        }while ("y".equals(answer));

    }

    private void rank() {
        //首先找到排序的数组，找到排序的数据
        ShareBikeCompany[] temparr = Arrays.copyOf(this.shareBikeCompanies, shareBikeCompanies.length);
        Arrays.sort(temparr,(a,b) -> b.getCount() - a.getCount());
        System.out.println("|公司名称|借出次数");
        for (int i = 0; i < temparr.length; i++) {
            System.out.print(temparr[i].getBikeCompanyName() + "");
            System.out.print(temparr[i].getCount() + "");
            System.out.println();
        }





    }

    private void removeBike() {
        int borrowCompany = getBorrowCompany();
        System.out.println("请输入你要删除的车的ID");
        int bikeId = scanner.nextInt();
        switch (borrowCompany){
            case 1:
                shareBikeCompanies[0].DeleteBike(bikeId);
                break;
            case 2:
                shareBikeCompanies[1].DeleteBike(bikeId);
                break;
            case 3:
                shareBikeCompanies[2].DeleteBike(bikeId);
                break;

        }
    }

    private void backBike() {
        int borrowCompany = getBorrowCompany();
        System.out.println("请输入你要还的车的ID");
        int bikeId = scanner.nextInt();
        switch (borrowCompany){
            case 1:
                shareBikeCompanies[0].BackBike(bikeId);
                break;
            case 2:
                shareBikeCompanies[1].BackBike(bikeId);
                break;
            case 3:
                shareBikeCompanies[2].BackBike(bikeId);
                break;
        }



    }

    private static int getBorrowCompany() {
        System.out.println("你要换的是哪一家公司的车？请输入" +
                "1：ofo" +
                "2: mobai"
        +"3:haluo");
        int borrowCompany = scanner.nextInt();
        return borrowCompany;
    }

    private void borrowBike() {
        System.out.println("你想借哪一家公司的车？请输入1：OFO,2:mobai,3:haluo");
        int borrowCompany = scanner.nextInt();
        System.out.println("请输入你要借的车的ID");
        int borrowNum = scanner.nextInt();
        switch (borrowCompany){
            case 1:
                shareBikeCompanies[0].BorrowBike(borrowNum);
                break;
            case 2:
                shareBikeCompanies[1].BorrowBike(borrowNum);
                break;
            case 3:
                shareBikeCompanies[2].BorrowBike(borrowNum);
                break;
        }

    }

    private void queryBike() {
        for (int i = 0; i < shareBikeCompanies.length; i++) {
            System.out.println("|公司id|公司名称|单车数量|借出数量|");
            System.out.print("\t"+ shareBikeCompanies[i].getBikeCompanyId());
            System.out.print("\t"+shareBikeCompanies[i].getBikeCompanyName()+"\t  ");
            System.out.print("\t"+shareBikeCompanies[i].getSum()+"\t  ");
            System.out.print("\t"+shareBikeCompanies[i].getCount()+"\t ");
            System.out.println();
            //开始打印单车信息
            ShareBike[] shareBikes = shareBikeCompanies[i].getShareBikes();
            System.out.println("|单车ID|单车状态|单车借出时间");
            for (int j = 0; j < shareBikeCompanies[i].getSum(); j++) {
                System.out.print("\t"+shareBikes[j].getBid() + "\t");
                System.out.print("\t"+shareBikes[j].getStatus() + "\t");
                System.out.print("\t"+shareBikes[j].getBorrowTime() + "\t");
                System.out.println();
            }



        }


    }

    private void init() {
        shareBikeCompanies = new ShareBikeCompany[3];
        shareBikeCompanies[0]=new ofoCompany("ofo",1,new ShareBike[5],0,0);
        shareBikeCompanies[1]=new haluoCompany("haluo",2,new ShareBike[5],0,0);
        shareBikeCompanies[2]=new mobaiCompany("mobai",3,new ShareBike[5],0,0);


    }

    private void addBiike(){
        System.out.println("1 ofo\n" +
                "2 haluo\n"+
                "3 mobai\n"+
                "请输入要投放的公司名字");
        int companyName  = scanner.nextInt();
        System.out.println("请输入要投入公司的数量");
        int bikeNum = scanner.nextInt();
        switch(companyName){
            case 1:
                shareBikeCompanies[0].addBike(bikeNum);
                break;
            case 2:
                shareBikeCompanies[1].addBike(bikeNum);
                break;
            case 3:
                shareBikeCompanies[2].addBike(bikeNum);
                break;
            default:
                System.out.println("请输入数字1-3");

        }


    }


}
